 /**
  ********************************************
  Tree-Node class
  ********************************************
  
 * This program uses "Node" in conjucation with "StorageTree" for Homework 4
 * 
 * This class comes with a "remove" method on top of what was already here
 * 
 * @author (Greg Shevach & Chris Nerf) 
 * @version (0)
 * @date (11/6/2011)
 * 
 * @updated (12/12/11)
 */

public class DoubleNode {

	//this contains the character being stored
    private String data;
    //Huffman probability
    //the value of the two lower children combined
    private int probability = 0;    
    //Huffman code
    private String huffmanCode;
    //values less than current node
    private DoubleNode nextLeft;
    //values greater than current node
    private DoubleNode nextRight;

    //constructor with null or 0 values
    public DoubleNode() {
	data = null;
	huffmanCode = null;
	nextLeft = null;
	nextRight = null;
    }

    //constructor with a value
    public DoubleNode( String element ) {
	data = element;
	huffmanCode = null;
	nextLeft = null;
	nextRight = null;
    }
    
    // returns reference to the next left object
    public DoubleNode getNextLeft()
    {
	return ( nextLeft );
    }
    
    //returns reference to the next right object
    public DoubleNode getNextRight() {
	return ( nextRight );
    }
    
    //set the huffman probability
    public void setProbability()
    {
    	int left = 0;
    	int right = 0;
    	
    	try{
    		left = nextLeft.probability;
    	}catch(Exception wtf)
    	{
    		left = 0;
    	}
    	try{
    		right = nextRight.probability;
    	}catch(Exception wtf)
    	{
    		right = 0;
    	}
    	
    	if(right > 0 && left > 0)
    		this.probability = right + left;
    		
    	
    }

    //sets the value of the next left node
    public void setNextLeft( DoubleNode node ) {
	nextLeft = node;
    }
    
    //sets the value of the next right node
    public void setNextRight( DoubleNode node ) {
    	nextRight = node;
        }
    
    //gets the element value
    public String getElement() {
	return ( data );
    }

    //sets the value of this node to element of type T
    public void setElement( String element ) {
	data = element;
    }
    
    public String toString() {

	return ( data.toString() );
    }
    
    public String getHuffmanCode()
    {
    	return this.huffmanCode;
    }
    
    public void setHuffmanCode(String huffmanCode)
    {
    	this.huffmanCode = huffmanCode;
    }
    
    public void print(DoubleNode root)
    {
    	//if the next 2 nodes are both null, you've gone too far
    	if(root.getNextLeft() == null && root.getNextRight() == null)
    		return;
    	else //print the value of the left node
    		System.out.println((char)Integer.parseInt(root.getNextLeft().getElement()) + "\t\t" + root.getNextLeft().getHuffmanCode());
    	//get the next right node
    	root.print(root.getNextRight());
    }

}

